package cn.edu.gduf.luowowo.mgrsite.web.controller;

import cn.edu.gduf.luowowo.mgrsite.util.UploadUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@Controller
public class UploadController {

    @Value("${file.path}")
    private String baseBath;

    @RequestMapping("/uploadImg")
    @ResponseBody
    public String uploadImg(MultipartFile pic) {
        return UploadUtil.upload(pic, baseBath);
    }

    @RequestMapping("/uploadImg_ck")
    @ResponseBody //按照插件要求的格式，返回json数据
    public Object uploadImg_ck(MultipartFile upload) {
        //处理富文本编辑器上传过来的图片
        Map<String, Object> map = new HashMap<>();
        try {
            String url = UploadUtil.upload(upload, baseBath);
            map.put("uploaded", 1);
            map.put("url", "/" + url);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> error = new HashMap<>();
            error.put("message", "上传失败");
            map.put("uploaded", 0);
            map.put("error", error);
        }
        return map;
    }
}
